package com.beasts.ant.page.service;

import com.beasts.ant.page.entity.APPagePortlet;
import com.beasts.ant.page.repository.PagePortletRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author cn_zkn
 * @date 20/04/2018 11:02 AM
 * @since 1.0
 */
@Service
public class PagePortletServiceImpl implements IPagePortletService {


    @Autowired
    private PagePortletRepository pagePortletRepository;


    @Override
    public List<APPagePortlet> findAll() {
        return pagePortletRepository.findAll();
    }

    /**
     * 保存频道布局
     */
    @Override
    public boolean savePagePortlet( String pageId, String layoutId, String portletId, String portletName) {
       try {
           APPagePortlet apPagePortlet = new APPagePortlet();
           Date currentTime = new Date();
           SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
           String dateString = formatter.format(currentTime);
           int randomNum = (int)(Math.random()*10000);
           String randomStr = String.valueOf(randomNum);
           dateString += randomStr;
           apPagePortlet.setId(dateString);
           apPagePortlet.setPageId(pageId);
           apPagePortlet.setLayoutId(layoutId);
           apPagePortlet.setPortletId(portletId);
           apPagePortlet.setPortletName(portletName);
           pagePortletRepository.save(apPagePortlet);
       }catch(Exception e){
           throw new RuntimeException(e);

       }
        return true;

    }
    /**
     * 删除频道布局
     */
    @Override
    public boolean deletePagePortlet(String pageId) {
        pagePortletRepository.deleteByPageId(pageId);
        return true;
    }
    /**
     * 根据pageID查询频道布局
     */
    @Override
    public List<APPagePortlet> getPagePortletByPageId(String pageId) {
        return pagePortletRepository.findByPageId(pageId);
    }


}
